package com.dream.cloud.controller;

import com.dream.cloud.entities.PayDTO;
import com.dream.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class OrderController {

//    public static final String PAYMENT_SRV_URL = "http://localhost:8001";

    public static final String PAYMENT_SRV_URL = "http://cloud-payment-service";//服务注册中心上的微服务名称
    @Resource
    private RestTemplate restTemplate;


    /**
     * 下单
     * @param payDTO
     * @return
     */
    @PostMapping("/consumer/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {
        return restTemplate.postForObject(PAYMENT_SRV_URL + "/pay/add", payDTO, ResultData.class);
    }

    /**
     * 通过id获取支付流水
     * @param id
     * @return
     */
    @GetMapping("/consumer/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(PAYMENT_SRV_URL + "/pay/get/" + id, ResultData.class);
    }

    /**
     * 获取所有支付流水
     * @return
     */
    @GetMapping("/consumer/pay/getAll")
    public ResultData getPayInfo() {
        return restTemplate.getForObject(PAYMENT_SRV_URL + "/pay/getAll", ResultData.class);
    }

    /**
     * 更新支付流水
     * @param payDTO
     * @return
     */
    @PutMapping("/consumer/pay/update")
    public ResultData updatePayInfo(@RequestBody PayDTO payDTO) {
        return restTemplate.exchange(PAYMENT_SRV_URL + "/pay/update", HttpMethod.PUT, new HttpEntity<>(payDTO), ResultData.class).getBody();
    }


    @DeleteMapping("/consumer/pay/delete/{id}")
    public ResultData deletePayInfo(@PathVariable("id") Integer id) {
        return restTemplate.exchange(PAYMENT_SRV_URL + "/pay/delete/" + id, HttpMethod.DELETE, HttpEntity.EMPTY, ResultData.class).getBody();
    }

    @GetMapping(value = "/consumer/pay/getConfig")
    private String getConfigByConsul()
    {
        return restTemplate.getForObject(PAYMENT_SRV_URL + "/pay/getConfig", String.class);
    }


    @Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/consumer/discovery")
    public String discovery()
    {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            System.out.println(element);
        }

        System.out.println("===================================");

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance element : instances) {
            System.out.println(element.getServiceId()+"\t"+element.getHost()+"\t"+element.getPort()+"\t"+element.getUri());
        }

        return instances.get(0).getServiceId()+":"+instances.get(0).getPort();
    }

}
